package org.lendoo.web.thread;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.lendoo.web.entity.User;
import org.lendoo.web.mapper.UserMapper;

import java.util.concurrent.Callable;
import java.util.Date;

/**
 * 添加或更新User记录
 */
public class SaveOrUpdateUserThread implements Callable<String> {

    private User user;
    private UserMapper userMapper;

    public SaveOrUpdateUserThread() {
    }

    public SaveOrUpdateUserThread(User user, UserMapper userMapper) {
        this.user = user;
        this.userMapper = userMapper;
    }

    @Override
    public String call() throws Exception {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getOpenid, user.getOpenid());
        User theUser = userMapper.selectOne(lambdaQueryWrapper);
        if (null != theUser){
            // 更新User对象
            user.setId(theUser.getId());
            userMapper.updateById(user);
        } else {
            // 插入User对象
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }
        return null;
    }
}
